﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace StructureModelDeduction.Forms
{
	public partial class LinkInstanceListForm : System.Windows.Forms.Form
	{
		public List<ElementId> LstLinkId
		{
			get
			{
				return this.m_lstLinkId;
			}
			set
			{
				this.m_lstLinkId = value;
			}
		}

		public LinkInstanceListForm(List<ElementId> lstElem, Document doc)
		{
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			this.readFileData();
			this.dataGridViewFile.Rows.Clear();
			foreach (ElementId elementId in lstElem)
			{
				RevitLinkInstance revitLinkInstance = doc.GetElement(elementId) as RevitLinkInstance;
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				dataGridViewRow.CreateCells(this.dataGridViewFile);
				dataGridViewRow.Cells[0].Value = elementId;
				dataGridViewRow.Cells[1].Value = revitLinkInstance.GetLinkDocument().Title;
				dataGridViewRow.Cells[2].Value = 0;
				this.dataGridViewFile.Rows.Add(dataGridViewRow);
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			foreach (object obj in ((IEnumerable)this.dataGridViewFile.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				if (dataGridViewRow.Cells[2].Value.ToString() == "1")
				{
					this.LstLinkId.Add(dataGridViewRow.Cells[0].Value as ElementId);
				}
			}
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			this.LstLinkId.Clear();
		}

		protected override bool ProcessKeyEventArgs(ref Message m)
		{
			int num = 256;
			int num2 = 27;
			if (m.Msg == num && m.WParam.ToInt32() == num2)
			{
				base.Close();
			}
			return base.ProcessKeyEventArgs(ref m);
		}

		private void readFileData()
		{
			string userDataLocation = Product.UserDataLocation;
			string path = userDataLocation + "\\LinkInstanceListForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				return;
			}
			try
			{
				FileStream fileStream = new FileStream(path, FileMode.Open);
				StreamReader streamReader = new StreamReader(fileStream);
				streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
				base.StartPosition = FormStartPosition.Manual;
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new System.Drawing.Point(x, y);
				streamReader.Close();
				fileStream.Close();
			}
			catch (Exception)
			{
				throw;
			}
		}

		private void WriteFileData()
		{
			string userDataLocation = Product.UserDataLocation;
			string path = userDataLocation + "\\LinkInstanceListForm.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			int x = base.Location.X;
			int y = base.Location.Y;
			streamWriter.WriteLine(x.ToString());
			streamWriter.WriteLine(y.ToString());
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void LinkInstanceListForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.WriteFileData();
		}

		private List<ElementId> m_lstLinkId = new List<ElementId>();
	}
}
